package com.sto.stoapp.activity.administrator.hardware;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.widget.StoHomeActivity;

import sto.android.app.StoPdaKeyEvent;

/**
 * 背光测试
 * 
 * @author mgf
 * 
 */
@SuppressLint("HandlerLeak")
public class AdminBackLightActivity extends StoHomeActivity {
	private static final int MAX_BRIGHTNESS = 255;

	private static final int MIN_BRIGHTNESS = 10;

	private static final int LIGHT_DELAY_TIME = 2000;

	private static final int DARK_STATE = 0xde;

	private static final int LIGHT_STATE = 0xdd;

	private static final int POOR_TEST_END = 0xda;

	private TextView backPrompt;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case DARK_STATE:
				backPrompt.setVisibility(View.GONE);
				setScreenBrightness(MIN_BRIGHTNESS);
				handler.sendEmptyMessageDelayed(LIGHT_STATE, LIGHT_DELAY_TIME);
				break;
			case LIGHT_STATE:
				setScreenBrightness(MAX_BRIGHTNESS);
				handler.sendEmptyMessageDelayed(POOR_TEST_END, LIGHT_DELAY_TIME);
				break;
			case POOR_TEST_END:
				finish();
				break;
			}
		}

	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_back_light);
		this.setTitleBottom(R.string.back_light,
				HomeBroadAction.ADMINISTRATOR_BROAD);
		backPrompt = (TextView) findViewById(R.id.back_prompt);
		handler.sendEmptyMessageDelayed(DARK_STATE, LIGHT_DELAY_TIME);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		handler.removeMessages(DARK_STATE);
		handler.removeMessages(LIGHT_STATE);
		handler.removeMessages(POOR_TEST_END);
	}

	// 设置屏幕亮度
	private void setScreenBrightness(int value) {
		WindowManager.LayoutParams params = this.getWindow().getAttributes();
		params.screenBrightness = value / 255f;
		this.getWindow().setAttributes(params);
	}
	
	// 添加小退功能
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
	    if (keyCode == StoPdaKeyEvent.KEYCODE_DEL) {
	        this.finish();
	    }
	    return super.onKeyUp(keyCode, event);
	}
	
	
}
